//#include - tells the compiler to add code here //.h - header ... at the top #include #include using namespace std; //int list[] - an integer array paramter //list - is not a copy of the argument void getValuesFromUser(int list[], int size) { for(int i = 0; i < size; i++) { cin >> list[i]; } } void displayList(int list[], int size) { for (int i = 0; i < size; i++) { cout << list[i] << endl; } } int smallestInList(int list[], int size) { //assume tht the first number is the smallest number int result = list[0]; //123,22,3,53,2334,1,3,4,5,3 for (int i = 1; i < size; i++) { if (list[i] < result) { result = list[i]; } } return result; } void reverseList(int list[], int size) { for (int i = 0; i < size; i++) { int j = size - 1 - i; //trade the values in the list at indexes i & j int temp = list[i]; list[i] = list[j]; list[j] = temp; } } //selection sort //void sortList(int list[], int size) //{ // for (int i = 0; i < size; i++) // { // int indexOfSmallest = i; // for (int j = i + 1; j < size; j++) // { // if (list[j] < list[indexOfSmallest]) // { // indexOfSmallest = j; // } // } // int temp = list[i]; // list[i] = list[indexOfSmallest]; // list[indexOfSmallest] = temp; // } //} void sortList(int list[], int size) { //size = 10 for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (list[j] > list[j + 1]) { //they are in the wrong order, swap them int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } void main() { int A[10]; //0-9 getValuesFromUser(A,10); displayList(A, 10); cout << "The smallest one is " << smallestInList(A, 10) << endl; sortList(A, 10); cout << "Sorted List" << endl; displayList(A, 10); }